<template>
	<view :class="['text_wrap', {'status_bar_height': statusHeight} ]" v-show="show">
		<view class="text_box">
			<view class="text_header">
				<!-- <image class="return_icon" src="/static/images/black_right_arrow@2x.png" @tap="closeDialog">
				</image> -->
				<view @tap="closeDialog">取消</view>
				<view @tap="saveText">保存</view>
			</view>
			<view class="text_title">{{title}}</view>
			<view class="text_val">
				<textarea :value="areaTextValue" :maxlength="maxlength" :auto-focus="show" :focus="show" :placeholder="placeholder"
					@input="getAreaText"></textarea>
				<view class="text_length">
					<text>{{areaTextValue.length}}</text>/{{maxlength}}字
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			show: {
				type: Boolean,
				default: false
			},
			value: {
				type: [Number, String],
				default: ''
			},
			// 限制输入长度
			maxlength: {
				type: [Number, String],
				default: 200
			},
			// 标题
			title: {
				type: [Number, String],
				default: '留言'
			},
			placeholder: {
				type: [Number, String],
				default: '请输入您的留言'
			},
			// APP是否设置状态栏间距
			statusHeight: {
				type: Boolean,
				default: true
			}
		},
		data() {
			return {
				areaTextValue: '',
			}
		},
		watch: {
			// 同步传入的默认文本数据
			value: {
				immediate: true,
				handler(value) {
					this.areaTextValue = value
				}
			},
			show() {
				this.areaTextValue = this.value
			}
		},
		methods: {
			// 获取输入框文本
			getAreaText(e) {
				this.areaTextValue = e.target.value
			},
			// 保存文本，将输入的文本传出
			saveText() {
				this.$emit("input", this.areaTextValue);
				this.closeDialog()
			},
			// 关闭弹窗
			closeDialog() {
				this.$emit("update:show", false);
			},
		}
	}
</script>

<style lang="scss" scoped>
	.status_bar_height {
		padding-top: calc(var(--status-bar-height));
	}
	.text_wrap {
		width: 100%;
		height: 100vh;
		position: fixed;
		top: 0;
		left: 0;
		z-index: 999;
		background-color: #fff;

		.text_box {
			padding: 0 34rpx;
		}

		.text_header {
			display: flex;
			align-items: center;
			justify-content: space-between;
			height: 88rpx;
			font-size: 28rpx;
			color: #33c2b4;

			.return_icon {
				width: 40rpx;
				height: 40rpx;
			}
		}

		.text_title {
			padding: 48rpx 0 32rpx 0;
			font-size: 32rpx;
			color: #1b2120;
			font-weight: bold;
			border-bottom: 1rpx solid #e4e6e6;

		}

		.text_val {
			padding: 32rpx 0 0 0;

			textarea {
				margin-bottom: 10rpx;
				word-break: break-word;
				overflow: hidden;
			}

			.text_length {
				width: 100%;
				height: 40rpx;
				position: fixed;
				right: 0;
				bottom: 0;
				padding: 0rpx 34rpx;
				background-color: #f4f5f6;
				padding-bottom: env(safe-area-inset-bottom);
				padding-bottom: constant(safe-area-inset-bottom);
				text-align: right;
				font-size: 24rpx;
				color: #1b2120;
				line-height: 40rpx;

				text {
					color: #ba6363;
				}
			}
		}
	}

	.textarea-placeholder {
		color: #cacccc;
		font-size: 28rpx;
	}
</style>